home *** CD-ROM | disk | FTP | other *** search
/ PC World 2007 September / PCWorld_2007-09_cd.bin / komunikace / firefox / Firefox Setup 2.0.0.6.exe / nonlocalized / components / nsSessionStartup.js < prev    next >
Encoding:
Text File  |  2007-07-25  |  15.8 KB  |  459 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the nsSessionStore component.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Simon B├╝nzli <zeniko@gmail.com>
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2006
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  * Dietrich Ayala <autonome@gmail.com>
  24.  *
  25.  * Alternatively, the contents of this file may be used under the terms of
  26.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  27.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28.  * in which case the provisions of the GPL or the LGPL are applicable instead
  29.  * of those above. If you wish to allow use of your version of this file only
  30.  * under the terms of either the GPL or the LGPL, and not to allow others to
  31.  * use your version of this file under the terms of the MPL, indicate your
  32.  * decision by deleting the provisions above and replace them with the notice
  33.  * and other provisions required by the GPL or the LGPL. If you do not delete
  34.  * the provisions above, a recipient may use your version of this file under
  35.  * the terms of any one of the MPL, the GPL or the LGPL.
  36.  *
  37.  * ***** END LICENSE BLOCK ***** */
  38.  
  39. /**
  40.  * Session Storage and Restoration
  41.  * 
  42.  * Overview
  43.  * This service reads user's session file at startup, and makes a determination 
  44.  * as to whether the session should be restored. It will restore the session 
  45.  * under the circumstances described below.
  46.  * 
  47.  * Crash Detection
  48.  * The session file stores a session.state property, that 
  49.  * indicates whether the browser is currently running. When the browser shuts 
  50.  * down, the field is changed to "stopped". At startup, this field is read, and
  51.  * if it's value is "running", then it's assumed that the browser had previously
  52.  * crashed, or at the very least that something bad happened, and that we should
  53.  * restore the session.
  54.  * 
  55.  * Forced Restarts
  56.  * In the event that a restart is required due to application update or extension
  57.  * installation, set the browser.sessionstore.resume_session_once pref to true,
  58.  * and the session will be restored the next time the browser starts.
  59.  * 
  60.  * Always Resume
  61.  * This service will always resume the session if the integer pref 
  62.  * browser.startup.page is set to 3.
  63.  */
  64.  
  65. /* :::::::: Constants and Helpers ::::::::::::::: */
  66.  
  67. const Cc = Components.classes;
  68. const Ci = Components.interfaces;
  69. const Cr = Components.results;
  70.  
  71. const CID = Components.ID("{ec7a6c20-e081-11da-8ad9-0800200c9a66}");
  72. const CONTRACT_ID = "@mozilla.org/browser/sessionstartup;1";
  73. const CLASS_NAME = "Browser Session Startup Service";
  74.  
  75. const STATE_RUNNING_STR = "running";
  76.  
  77. /* :::::::: Pref Defaults :::::::::::::::::::: */
  78.  
  79. // whether the service is enabled
  80. const DEFAULT_ENABLED = true;
  81.  
  82. // resume the current session at startup just this once
  83. const DEFAULT_RESUME_SESSION_ONCE = false;
  84.  
  85. // resume the current session at startup if it had previously crashed
  86. const DEFAULT_RESUME_FROM_CRASH = true;
  87.  
  88. function debug(aMsg) {
  89.   aMsg = ("SessionStartup: " + aMsg).replace(/\S{80}/g, "$&\n");
  90.   Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService)
  91.                                      .logStringMessage(aMsg);
  92. }
  93.  
  94. /* :::::::: The Service ::::::::::::::: */
  95.  
  96. function SessionStartup() {
  97. }
  98.  
  99. SessionStartup.prototype = {
  100.  
  101.   // the state to restore at startup
  102.   _iniString: null,
  103.  
  104. /* ........ Global Event Handlers .............. */
  105.  
  106.   /**
  107.    * Initialize the component
  108.    */
  109.   init: function sss_init() {
  110.     this._prefBranch = Cc["@mozilla.org/preferences-service;1"].
  111.                        getService(Ci.nsIPrefService).
  112.                        getBranch("browser.");
  113.     this._prefBranch.QueryInterface(Ci.nsIPrefBranch2);
  114.  
  115.     // if the service is disabled, do not init 
  116.     if (!this._getPref("sessionstore.enabled", DEFAULT_ENABLED))
  117.       return;
  118.  
  119.     // get file references
  120.     var dirService = Cc["@mozilla.org/file/directory_service;1"].
  121.                      getService(Ci.nsIProperties);
  122.     this._sessionFile = dirService.get("ProfD", Ci.nsILocalFile);
  123.     this._sessionFile.append("sessionstore.js");
  124.     
  125.     // only read the session file if config allows possibility of restoring
  126.     var resumeFromCrash = this._getPref("sessionstore.resume_from_crash", DEFAULT_RESUME_FROM_CRASH);
  127.     if (resumeFromCrash || this._doResumeSession()) {
  128.       // get string containing session state
  129.       this._iniString = this._readFile(this._sessionFile);
  130.       if (this._iniString) {
  131.         try {
  132.           // parse the session state into JS objects
  133.           var s = new Components.utils.Sandbox("about:blank");
  134.           var initialState = Components.utils.evalInSandbox(this._iniString, s);
  135.  
  136.           // set bool detecting crash
  137.           this._lastSessionCrashed =
  138.             initialState.session && initialState.session.state &&
  139.             initialState.session.state == STATE_RUNNING_STR;
  140.         // invalid .INI file - nothing can be restored
  141.         }
  142.         catch (ex) { debug("The session file is invalid: " + ex); } 
  143.       }
  144.     }
  145.     
  146.     // prompt and check prefs
  147.     this._doRestore = this._lastSessionCrashed ? this._doRecoverSession() : this._doResumeSession();
  148.     if (this._iniString && !this._doRestore) {
  149.       this._iniString = null; // reset the state string
  150.     }
  151.     if (this._getPref("sessionstore.resume_session_once", DEFAULT_RESUME_SESSION_ONCE)) {
  152.       this._prefBranch.setBoolPref("sessionstore.resume_session_once", false);
  153.     }
  154.     
  155.     if (this.doRestore()) {
  156.       // wait for the first browser window to open
  157.       var observerService = Cc["@mozilla.org/observer-service;1"].
  158.                             getService(Ci.nsIObserverService);
  159.       observerService.addObserver(this, "domwindowopened", true);
  160.     }
  161.   },
  162.  
  163.   /**
  164.    * Handle notifications
  165.    */
  166.   observe: function sss_observe(aSubject, aTopic, aData) {
  167.     var observerService = Cc["@mozilla.org/observer-service;1"].
  168.                           getService(Ci.nsIObserverService);
  169.  
  170.     switch (aTopic) {
  171.     case "app-startup": 
  172.       observerService.addObserver(this, "final-ui-startup", true);
  173.       break;
  174.     case "final-ui-startup": 
  175.       observerService.removeObserver(this, "final-ui-startup");
  176.       this.init();
  177.       break;
  178.     case "domwindowopened":
  179.       var window = aSubject;
  180.       var self = this;
  181.       window.addEventListener("load", function() {
  182.         self._onWindowOpened(window);
  183.         window.removeEventListener("load", arguments.callee, false);
  184.       }, false);
  185.       break;
  186.     }
  187.   },
  188.  
  189.   /**
  190.    * Removes the default arguments from the first browser window
  191.    * (and removes the "domwindowopened" observer afterwards).
  192.    */
  193.   _onWindowOpened: function sss_onWindowOpened(aWindow) {
  194.     var wType = aWindow.document.documentElement.getAttribute("windowtype");
  195.     if (wType != "navigator:browser")
  196.       return;
  197.     
  198.     /**
  199.      * Note: this relies on the fact that nsBrowserContentHandler will return
  200.      * a different value the first time it's getter is called after an update,
  201.      * due to its needHomePageOverride() logic. We don't want to remove the
  202.      * default arguments in the update case, since they include the "What's
  203.      * New" page.
  204.      *
  205.      * Since we're garanteed to be at least the second caller of defaultArgs
  206.      * (nsBrowserContentHandler calls it to determine which arguments to pass
  207.      * at startup), we know that if the window's arguments don't match the
  208.      * current defaultArguments, we're either in the update case, or we're
  209.      * launching a non-default browser window, so we shouldn't remove the
  210.      * window's arguments.
  211.      */
  212.     var defaultArgs = Cc["@mozilla.org/browser/clh;1"].
  213.                       getService(Ci.nsIBrowserHandler).defaultArgs;
  214.     if (aWindow.arguments && aWindow.arguments[0] &&
  215.         aWindow.arguments[0] == defaultArgs)
  216.       aWindow.arguments[0] = null;
  217.     
  218.     var observerService = Cc["@mozilla.org/observer-service;1"].
  219.                           getService(Ci.nsIObserverService);
  220.     observerService.removeObserver(this, "domwindowopened");
  221.   },
  222.  
  223. /* ........ Public API ................*/
  224.  
  225.   /**
  226.    * Get the session state as a string
  227.    */
  228.   get state() {
  229.     return this._iniString;
  230.   },
  231.  
  232.   /**
  233.    * Determine whether there is a pending session restore.
  234.    * @returns bool
  235.    */
  236.   doRestore: function sss_doRestore() {
  237.     return this._doRestore && this._iniString != null;
  238.   },
  239.  
  240. /* ........ Auxiliary Functions .............. */
  241.  
  242.   /**
  243.    * Whether or not to resume session, if not recovering from a crash.
  244.    * @returns bool
  245.    */
  246.   _doResumeSession: function sss_doResumeSession() {
  247.     return this._getPref("startup.page", 1) == 3 ||
  248.       this._getPref("sessionstore.resume_session_once", DEFAULT_RESUME_SESSION_ONCE);
  249.   },
  250.  
  251.   /**
  252.    * prompt user whether or not to restore the previous session,
  253.    * if the browser crashed
  254.    * @returns bool
  255.    */
  256.   _doRecoverSession: function sss_doRecoverSession() {
  257.     // do not prompt or resume, post-crash
  258.     if (!this._getPref("sessionstore.resume_from_crash", DEFAULT_RESUME_FROM_CRASH))
  259.       return false;
  260.  
  261.     // if the prompt fails, recover anyway
  262.     var recover = true;
  263.     // allow extensions to hook in a more elaborate restore prompt
  264.     // XXXzeniko drop this when we're using our own dialog instead of a standard prompt
  265.     var dialogURI = this._getPref("sessionstore.restore_prompt_uri");
  266.     
  267.     try {
  268.       if (dialogURI) { // extension provided dialog 
  269.         var params = Cc["@mozilla.org/embedcomp/dialogparam;1"].
  270.                      createInstance(Ci.nsIDialogParamBlock);
  271.         // default to recovering
  272.         params.SetInt(0, 0);
  273.         Cc["@mozilla.org/embedcomp/window-watcher;1"].
  274.         getService(Ci.nsIWindowWatcher).
  275.         openWindow(null, dialogURI, "_blank", 
  276.                    "chrome,modal,centerscreen,titlebar", params);
  277.         recover = params.GetInt(0) == 0;
  278.       }
  279.       else { // basic prompt with no options
  280.         // get app name from branding properties
  281.         var brandStringBundle = this._getStringBundle("chrome://branding/locale/brand.properties");
  282.         var brandShortName = brandStringBundle.GetStringFromName("brandShortName");
  283.  
  284.         // create prompt strings
  285.         var ssStringBundle = this._getStringBundle("chrome://browser/locale/sessionstore.properties");
  286.         var restoreTitle = ssStringBundle.formatStringFromName("restoredTitle", [brandShortName], 1);
  287.         var restoreText = ssStringBundle.formatStringFromName("restoredMsg", [brandShortName], 1);
  288.         var buttonTitle = ssStringBundle.GetStringFromName("buttonTitle");
  289.         var cancelTitle = ssStringBundle.GetStringFromName("cancelTitle");
  290.  
  291.         var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].
  292.                             getService(Ci.nsIPromptService);
  293.  
  294.         // set the buttons that will appear on the dialog
  295.         var flags = promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_0 +
  296.                     promptService.BUTTON_TITLE_IS_STRING * promptService.BUTTON_POS_1 +
  297.                     promptService.BUTTON_POS_0_DEFAULT;
  298.         
  299.         var buttonChoice = promptService.confirmEx(null, restoreTitle, restoreText, 
  300.                                           flags, buttonTitle, cancelTitle, null, 
  301.                                           null, {});
  302.         recover = (buttonChoice == 0);
  303.       }
  304.     }
  305.     catch (ex) { dump(ex + "\n"); } // if the prompt fails, recover anyway
  306.     return recover;
  307.   },
  308.  
  309.   /**
  310.    * Convenience method to get localized string bundles
  311.    * @param aURI
  312.    * @returns nsIStringBundle
  313.    */
  314.   _getStringBundle: function sss_getStringBundle(aURI) {
  315.     var bundleService = Cc["@mozilla.org/intl/stringbundle;1"].
  316.                         getService(Ci.nsIStringBundleService);
  317.     var appLocale = Cc["@mozilla.org/intl/nslocaleservice;1"].
  318.                     getService(Ci.nsILocaleService).getApplicationLocale();
  319.     return bundleService.createBundle(aURI, appLocale);
  320.   },
  321.  
  322. /* ........ Storage API .............. */
  323.  
  324.   /**
  325.    * basic pref reader
  326.    * @param aName
  327.    * @param aDefault
  328.    * @param aUseRootBranch
  329.    */
  330.   _getPref: function sss_getPref(aName, aDefault) {
  331.     var pb = this._prefBranch;
  332.     try {
  333.       switch (pb.getPrefType(aName)) {
  334.       case pb.PREF_STRING:
  335.         return pb.getCharPref(aName);
  336.       case pb.PREF_BOOL:
  337.         return pb.getBoolPref(aName);
  338.       case pb.PREF_INT:
  339.         return pb.getIntPref(aName);
  340.       default:
  341.         return aDefault;
  342.       }
  343.     }
  344.     catch(ex) {
  345.       return aDefault;
  346.     }
  347.   },
  348.  
  349.   /**
  350.    * reads a file into a string
  351.    * @param aFile
  352.    *        nsIFile
  353.    * @returns string
  354.    */
  355.   _readFile: function sss_readFile(aFile) {
  356.     try {
  357.       var stream = Cc["@mozilla.org/network/file-input-stream;1"].
  358.                    createInstance(Ci.nsIFileInputStream);
  359.       stream.init(aFile, 0x01, 0, 0);
  360.       var cvstream = Cc["@mozilla.org/intl/converter-input-stream;1"].
  361.                      createInstance(Ci.nsIConverterInputStream);
  362.       cvstream.init(stream, "UTF-8", 1024, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
  363.       
  364.       var content = "";
  365.       var data = {};
  366.       while (cvstream.readString(4096, data)) {
  367.         content += data.value;
  368.       }
  369.       cvstream.close();
  370.       
  371.       return content.replace(/\r\n?/g, "\n");
  372.     }
  373.     catch (ex) { } // inexisting file?
  374.     
  375.     return null;
  376.   },
  377.  
  378. /* ........ QueryInterface .............. */
  379.  
  380.   QueryInterface: function(aIID) {
  381.     if (!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIObserver) && 
  382.       !aIID.equals(Ci.nsISupportsWeakReference) && 
  383.       !aIID.equals(Ci.nsISessionStartup)) {
  384.       Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
  385.       return null;
  386.     }
  387.     
  388.     return this;
  389.   }
  390. };
  391.  
  392. /* :::::::: Service Registration & Initialization ::::::::::::::: */
  393.  
  394. /* ........ nsIModule .............. */
  395.  
  396. const SessionStartupModule = {
  397.  
  398.   getClassObject: function(aCompMgr, aCID, aIID) {
  399.     if (aCID.equals(CID)) {
  400.       return SessionStartupFactory;
  401.     }
  402.     
  403.     Components.returnCode = Cr.NS_ERROR_NOT_REGISTERED;
  404.     return null;
  405.   },
  406.  
  407.   registerSelf: function(aCompMgr, aFileSpec, aLocation, aType) {
  408.     aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
  409.     aCompMgr.registerFactoryLocation(CID, CLASS_NAME, CONTRACT_ID, aFileSpec, aLocation, aType);
  410.  
  411.     var catMan = Cc["@mozilla.org/categorymanager;1"].
  412.                  getService(Ci.nsICategoryManager);
  413.     catMan.addCategoryEntry("app-startup", CLASS_NAME, "service," + CONTRACT_ID, true, true);
  414.   },
  415.  
  416.   unregisterSelf: function(aCompMgr, aLocation, aType) {
  417.     aCompMgr.QueryInterface(Ci.nsIComponentRegistrar);
  418.     aCompMgr.unregisterFactoryLocation(CID, aLocation);
  419.  
  420.     var catMan = Cc["@mozilla.org/categorymanager;1"].
  421.                  getService(Ci.nsICategoryManager);
  422.     catMan.deleteCategoryEntry( "app-startup", "service," + CONTRACT_ID, true);
  423.   },
  424.  
  425.   canUnload: function(aCompMgr) {
  426.     return true;
  427.   }
  428. }
  429.  
  430. /* ........ nsIFactory .............. */
  431.  
  432. const SessionStartupFactory = {
  433.  
  434.   createInstance: function(aOuter, aIID) {
  435.     if (aOuter != null) {
  436.       Components.returnCode = Cr.NS_ERROR_NO_AGGREGATION;
  437.       return null;
  438.     }
  439.     
  440.     return (new SessionStartup()).QueryInterface(aIID);
  441.   },
  442.  
  443.   lockFactory: function(aLock) { },
  444.  
  445.   QueryInterface: function(aIID) {
  446.     if (!aIID.equals(Ci.nsISupports) && !aIID.equals(Ci.nsIModule) &&
  447.         !aIID.equals(Ci.nsIFactory) && !aIID.equals(Ci.nsISessionStartup)) {
  448.       Components.returnCode = Cr.NS_ERROR_NO_INTERFACE;
  449.       return null;
  450.     }
  451.     
  452.     return this;
  453.   }
  454. };
  455.  
  456. function NSGetModule(aComMgr, aFileSpec) {
  457.   return SessionStartupModule;
  458. }
  459.